package com.gxa.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.BusinessException;
import com.gxa.pojo.Brand;
import com.gxa.service.BrandService;
import com.gxa.utils.DataCheck;
import com.gxa.utils.JsonResult;
import io.swagger.annotations.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @program: saas
 * @description: 品牌接口
 * @author: Tirion
 * @create: 2020-09-21 19:05
 **/
@Controller
@CrossOrigin
@RequestMapping("/brand")
@ControllerAdvice
@Api(value = "品牌管理",tags = "品牌管理的api")
public class BrandController {

    @Resource
    private BrandService brandService;

    @PostMapping("/list")
    @ResponseBody
    @ExceptionHandler(BusinessException.class)
    @ApiOperation(value = "品牌列表", httpMethod = "post", response = String.class)
    @ApiImplicitParams({@ApiImplicitParam(name = "startIndex", value = "当前页",
            required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true,
                    dataType = "int", paramType = "query")
    })
    public Map<String, Object> list(@RequestParam(value = "startIndex",required = false,defaultValue = "0") Integer startIndex,
                                    @RequestParam(value = "pageSize",required = false,defaultValue = "0") Integer pageSize) {
        PageHelper.startPage(startIndex,pageSize);
        JsonResult jsonResult = new JsonResult();
        List<Brand> brands = brandService.queryAll();
        PageInfo<Brand> brandPageInfo = new PageInfo<>(brands);
        long total = brandPageInfo.getTotal();
        DataCheck.check(jsonResult, brands);
        jsonResult.put("total",total);
        return jsonResult.getValues();

    }

    @PostMapping("/save")
    @ResponseBody
    @ApiOperation(value = "添加品牌",httpMethod = "post",response = String.class)
    public Map<String, Object> save(@RequestBody @ApiParam(name = "brand",
    value = "品牌的pojo信息",required = true) Brand brand) {

        JsonResult jsonResult = new JsonResult();
        if (brand == null) {
            jsonResult.setMsg("数据为空");
            jsonResult.setCode("403");
            return jsonResult.getValues();
        }
        //判断品牌是否重复
        Brand brand1 = brandService.nameQuery(brand.getName());
        if (brand1 !=null) {
            jsonResult.setMsg("品牌已存在");
            jsonResult.setCode("403");
            return jsonResult.getValues();
        }
        Integer save = brandService.save(brand);
        DataCheck.check(jsonResult,save);
        return jsonResult.getValues();
    }

    @GetMapping("/delete")
    @ResponseBody
    @ApiOperation(value ="删除",httpMethod = "get",response = String.class)
    public Map<String, Object> delete(@RequestParam @ApiParam(name = "id",
            value = "目标品牌id",required = true) Integer id) {
        JsonResult jsonResult = new JsonResult();
        if (id == null) {
            jsonResult.setMsg("id不能为空");
            jsonResult.setCode("403");
            return jsonResult.getValues();
        }
        Integer integer = brandService.deleteById(id);
        DataCheck.check(jsonResult,integer);
        return jsonResult.getValues();
    }


    @PostMapping("/update")
    @ResponseBody
    @ApiOperation(value = "修改品牌",httpMethod = "post",response = String.class)
    public Map<String, Object> update(@RequestBody @ApiParam(name = "brand",
            value = "品牌的pojo信息",required = true) Brand brand) {
        JsonResult jsonResult = new JsonResult();
        if (brand == null) {
            jsonResult.setMsg("参数为空");
            jsonResult.setCode("430");
            return jsonResult.getValues();
        }
        Integer update = brandService.update(brand);
        DataCheck.check(jsonResult,update);
        return jsonResult.getValues();
    }

    @GetMapping("/selectById")
    @ResponseBody
    @ApiOperation(value = "根据id获取品牌",httpMethod = "get",response = String.class)
    public Map<String, Object> selectById(@RequestParam @ApiParam(name = "id",
            value = "目标品牌id",required = true) Integer id) {
        JsonResult jsonResult = new JsonResult();
        if (id == null) {
            jsonResult.setMsg("id不能为空");
            jsonResult.setCode("403");
            return jsonResult.getValues();
        }
        Brand brand = brandService.selectById(id);
        DataCheck.check(jsonResult,brand);
        return jsonResult.getValues();

    }

    @PostMapping("/fuzzy")
    @ResponseBody
    @ApiOperation(value = "关键字查询",httpMethod = "get",response = String.class)
    public Map<String, Object> fuzzyQuery(@RequestParam @ApiParam(name = "like",
            value = "查询的关键字",required = true) String like) {
        JsonResult jsonResult = new JsonResult();
        if (like == null) {
            jsonResult.setMsg("请输入查询关键字");
            jsonResult.setCode("403");
            return jsonResult.getValues();
        }
        List<Brand> brands = brandService.fuzzyQuery(like);
        DataCheck.check(jsonResult,brands);
        return jsonResult.getValues();
    }

    @PostMapping("/deleteBatch")
    @ResponseBody
    @ApiOperation(value ="批量删除",httpMethod = "get",response = String.class)
    public Map<String, Object> delete(@RequestBody @ApiParam(name = "list",
            value = "目标品牌id数组",required = true) List<Integer> list) {
        JsonResult jsonResult = new JsonResult();
        if (list == null) {
            jsonResult.setMsg("id不能为空");
            jsonResult.setCode("403");
            return jsonResult.getValues();
        }
        Integer integer = brandService.deleteBatch(list);
        DataCheck.check(jsonResult,integer);
        return jsonResult.getValues();
    }

}
